package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.example.demo.dao.UserDao;
import com.example.demo.databinding.ActivityMainBinding;
import com.example.demo.entity.User;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.MaybeSource;
import io.reactivex.rxjava3.functions.Action;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;


/**
 * @Desc
 * @Author zxp
 * @Date 2024/3/17
 */
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private ActivityMainBinding inflate;

    private AppDatabase db;

    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflate = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(inflate.getRoot());

        // 初始化数据库，指定数据库名称
        db = AppDatabase.getInstance(getApplicationContext());

        userDao = db.getUserDao();

        // 插入n个
        inflate.buttonInsertOne.setOnClickListener(v -> {

            /*userDao.insertUser(getUser())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Long>() {
                        @Override
                        public void accept(Long aLong) throws Throwable {
                            Log.d(TAG, "1成功" + aLong);
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Throwable {
                            Log.d(TAG, "1失败", throwable);
                        }
                    });*/
            userDao.insertUserList(new ArrayList<>(Collections.singleton(getUser())))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<List<Long>>() {
                        @Override
                        public void accept(List<Long> longs) throws Throwable {
                            Log.d(TAG, "1成功" + longs);
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Throwable {
                            Log.d(TAG, "1失败", throwable);
                        }
                    });
        });

        // 查询全部
        inflate.buttonGetAll.setOnClickListener(v -> {
            userDao.getAll()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<List<User>>() {
                        @Override
                        public void accept(List<User> users) throws Exception {
                            Log.d(TAG, "2成功：" + users);
                            inflate.textViewResult.setText(users.toString());
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            Log.d(TAG, "2失败",  throwable);
                        }
                    });
        });

        // 查询byid
        inflate.buttonGetById.setOnClickListener(v -> {
            Long id = null;
            if (inflate.editTextId.getText().toString().length() > 0) {
                try {
                    id = Long.valueOf(inflate.editTextId.getText().toString());
                } catch (Exception e) {

                }
            }
            userDao.getById(id)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doFinally(() -> Log.d(TAG, "3最后会执行"))
                    .doAfterSuccess(user -> Log.d(TAG, "3成功且有之后执行：" + user))
                    .doOnError(e -> Log.d(TAG, "3失败", e))
                    .doOnComplete(() -> Log.d(TAG, "3没有，查到null"))
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Throwable {
                            // 不为null
                            Log.d(TAG, "3成功且有：" + user);
                            inflate.textViewResult.setText(user.toString());
                        }
                    });
        });

        // 删除n个
        inflate.buttonDeleteUsers.setOnClickListener(v -> {
            userDao.deleteById(Long.valueOf(inflate.editTextId.getText().toString()))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe();
        });

        // 删除全部
        inflate.buttonDeleteAll.setOnClickListener(v -> {
            userDao.deleteAll()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe();
        });

        // 更新
        inflate.buttonUpdateOne.setOnClickListener(v -> {
            userDao.updateUser(getUser())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Integer>() {
                        @Override
                        public void accept(Integer integer) throws Throwable {
                            // 需要判断integer
                            Log.d(TAG, "6成功" + integer);
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Throwable {
                            Log.d(TAG, "6失败", throwable);
                        }
                    });
        });

        // 更新非空字段
        inflate.buttonUpdateOne2.setOnClickListener(v -> {
            userDao.updateUserIfNotNull(getUser())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Integer>() {
                        @Override
                        public void accept(Integer integer) throws Throwable {
                            Log.d(TAG, "7成功" + integer);
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Throwable {
                            Log.d(TAG, "7失败", throwable);
                        }
                    });
        });

    }

    private User getUser() {
        User user = new User();
        if (!inflate.editTextId.getText().toString().isEmpty()) {
            user.setId(Long.valueOf(inflate.editTextId.getText().toString()));
        }
        if (!inflate.editTextName.getText().toString().isEmpty()) {
            user.setName(inflate.editTextName.getText().toString());
        }
        if (!inflate.editTextAge.getText().toString().isEmpty()) {
            user.setAge(Integer.valueOf(inflate.editTextAge.getText().toString()));
        }
        return user;
    }
}